<?php

namespace Admin\Controller;
use User\Api\UserApi;

/**
 * 系统配置控制器
 * jackzheng
 */
class SystemController extends AdminController {



    /**
     * 商城系统配置首页
     *
     */
    public function config(){
		
		$res = $this->read_cache();
		$shop_config = json_decode($res,TRUE);

        $this->assign('shop_config',$shop_config);
        $this->display();
    }


    /**修改系统配置信息
     * @author leiying
     */
    public function update_config(){
        if(I('post.btn')){$this->success('');}
        if(IS_POST){
            $data = I('post.');

            if(!isset($data['experience_money']) || empty(trim($data['experience_money']))){
                $this->error("请填写注册奖励(体验金)");
            }
            if(!isset($data['invite_award_money']) || empty(trim($data['invite_award_money']))){
                $this->error("请填写邀请奖励(体验金)");
            }

            if(!isset($data['experience_day']) || empty(trim($data['experience_day']))){
                $this->error("请填写体验金收益周期");
            }
            if(!isset($data['settle_time']) || empty(trim($data['settle_time']))){
                $this->error("请填写收益结算时间点");
            }
            if(!isset($data['rate_range']) || empty(trim($data['rate_range']))){
                $this->error("请填写投资(预期年化收益率范围)");
            }
            if(!isset($data['client_phone']) || empty(trim($data['client_phone']))){
                $this->error("请填写客服电话");
            }

            /*处理参数*/
            $up_data = array(
                'experience_money'=>trim($data['experience_money']),
                'invite_award_money'=>trim($data['invite_award_money']),
                'experience_day'=>trim($data['experience_day']),
                'settle_time'=>trim($data['settle_time']),
                'rate_range'=>trim($data['rate_range']),
                'client_phone'=>trim($data['client_phone']),
            );

            $res= $this->cacheData($up_data);
            if($res){
                /*记录行为日志*/
//                $info['price']=$data['goods_price'];
//                $res = action_log('update_goods','order_goods',$info['id'],UID,$info);
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }
    }
	
	
	public function read_cache(){

		//文件路径名
		$_dir = $_SERVER['DOCUMENT_ROOT'].'/Mapi/public/config/';
        $file_name = $_dir.'shop_config.txt';
	    //读取缓存
        if(!is_file($file_name)){
            return false;
        }else{
            return file_get_contents($file_name);
        }
	 }
	
	
	public function cacheData($v = ''){
		
        //文件路径名
		$_dir = $_SERVER['DOCUMENT_ROOT'].'/Mapi/public/config/';
        $data_name = $_dir.'shop_config.txt';

        //$v不为‘’：存储缓存或者删除缓存
        if($v !== ''){

            //删除缓存
			$this->del_cache($data_name);

            //存储缓存
            $dir = dirname($data_name);
            if(!is_dir($dir)){
                mkdir($dir,0777);
            }
	
			$data = json_encode($v);
            return file_put_contents($data_name,$data);
        }

    }
	
	public function del_cache($filename){
        //文件名
		if(file_exists($filename)){
		 @unlink($filename);
		}
        
	}

}
